package com.sps.loadingdemo;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity {
    private List<RecyclerView> lists=new ArrayList<>();
    private ViewPager mVp;
    private TabLayout mTabLayout;
    private String[] navigations;
    private RecyclerView mRecyclerViewAll;
    private List<OrderBean.ObjectBean> mObjectBeanList;
    private LinearLayoutManager mManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVp = (ViewPager) findViewById(R.id.vp);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        //导航标签,tabLayout的数据源
        navigations = getResources().getStringArray(R.array.navigations);

        //根据当前获取的标题添加到tab中
        for(int i=0;i<navigations.length;i++){
             if(i==0){
                 mTabLayout.addTab(mTabLayout.newTab().setText(navigations[i]),true);
             }
            mTabLayout.addTab(mTabLayout.newTab().setText(navigations[i]),false);

        }
        //获取数据源
        initData();
        mManager = new LinearLayoutManager(this);
        mManager.setOrientation(LinearLayoutManager.VERTICAL);

        //ViewPager的数据源
        mRecyclerViewAll = new RecyclerView(this);
        mRecyclerViewAll.setLayoutManager(mManager);
        mRecyclerViewAll.setAdapter(new OrderAdapter(MainActivity.this,mObjectBeanList));

        //将rv添加到集合中
        RecyclerView recyclerViewPay = new RecyclerView(this);
        recyclerViewPay.setLayoutManager(mManager);

        RecyclerView recyclerViewSend = new RecyclerView(this);
        recyclerViewSend.setLayoutManager(mManager);

        RecyclerView recyclerViewReceive = new RecyclerView(this);
        recyclerViewReceive.setLayoutManager(mManager);

        RecyclerView recyclerViewComment = new RecyclerView(this);
        recyclerViewComment.setLayoutManager(mManager);


        lists.add(mRecyclerViewAll);
        lists.add(recyclerViewPay);
        lists.add(recyclerViewSend);
        lists.add(recyclerViewReceive);
        lists.add(recyclerViewComment);

        //关联适配器
        mVp.setAdapter(new NavigationAdapter(lists));
        //两者之间进行关联
        mTabLayout.setupWithViewPager(mVp,true);



    }

    /**
     * 初始化数据
     */
    private void initData() {

        OkHttpUtils.get()
                .url("http://192.168.0.117:8080/order/listOrder")
                .addParams("userId","1")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Logger.json(response);
                Gson gson=new Gson();
                OrderBean orderBean=gson.fromJson(response,OrderBean.class);
                String code=orderBean.getCode();
                if(!"200".equals(code)) return;
                //请求成功
                mObjectBeanList = orderBean.getObject();
                if(mObjectBeanList ==null) return;
                //集合中有数据
                Logger.json(response);


            }
        });


    }

    class NavigationAdapter extends PagerAdapter {
        List<RecyclerView> mViewList;
        public NavigationAdapter(List<RecyclerView> lists) {

            this.mViewList=lists;

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            return  mViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return navigations[position];
        }
    }



}
